/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.<br>
 */
package com.ce.pms.system.renovation.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.charge.model.BusinessCodeDomain;
import com.ce.pms.charge.service.IBusinessCodeService;
import com.ce.pms.charge.service.IParameterDefService;
import com.ce.pms.charge.vo.BusinessCodeVO;
import com.ce.pms.charge.vo.ParameterDefVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.renovation.dao.IRenovateChargeItemConfigDao;
import com.ce.pms.system.renovation.model.RenovateChargeItemConfigDO;
import com.ce.pms.system.renovation.service.IRenovateChargeItemConfigService;
import com.ce.pms.system.renovation.vo.RenovateChargeItemConfigVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述装修收费项目登记<br>
 * @author 易贤建 创建日期：2008-12-25<br>
 */
public class RenovateChargeItemConfigServiceImpl extends PmsBaseServiceImpl implements
		IRenovateChargeItemConfigService {
	/**
	 * 装修收费项目DAO
	 */
	private IRenovateChargeItemConfigDao renovateChargeItemConfigDao = null;
	/**
	 * 交易码server
	 */
	private IBusinessCodeService  businessCodeService= null;
	/**
	 * 费用参数定义表
	 */
	private IParameterDefService parameterDefService = null;

	private final String DEPOSIT_CODE = "DEPOSIT_CODE";
	/**
	 * 功能描述: 通过ID查询装修收费登记项目<br>
	 * @param keyword
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 */
	public RenovateChargeItemConfigVO getRenovateChargeItemConfigById(Long keyword){
		checkParameterIsNull(keyword, "system:approval_info_id_not_null");
		RenovateChargeItemConfigDO configDO = renovateChargeItemConfigDao.findRenovateChargeItemConfigById(keyword);
		checkParameterIsNull(configDO,"system:approval_info_object_not_found");//为空,没找到该对象
		return assembleD2V(configDO);
	}
	/**
	 * 功能描述:装修收费登记项目查询<br>
	 * @param searchVO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 */
	@SuppressWarnings("unchecked")
	public Page queryRenovateChargeItemList(SearchVO searchVO) {
		Page page = renovateChargeItemConfigDao.queryRenovateChargeItemList(searchVO);
		List<RenovateChargeItemConfigVO> result = assembleD2V(page.getResult());
		page.setResult(result);
		return page;
	}

	/**
	 * 功能描述: 添加装修收费登记项目<br>
	 * @param renovateChargeItemConfigVO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 */
	@SuppressWarnings("unchecked")
	public RenovateChargeItemConfigVO add(RenovateChargeItemConfigVO renovateChargeItemConfigVO){
		checkRenovateChargeItemConfigBaseInfo(renovateChargeItemConfigVO);
		validateItenName(renovateChargeItemConfigVO);

		RenovateChargeItemConfigDO configDO = assembleV2D(renovateChargeItemConfigVO);
		configDO.setSysInfo(super.createInfo(renovateChargeItemConfigVO.getCreator()));
		RenovateChargeItemConfigDO newConfigDO = (RenovateChargeItemConfigDO)renovateChargeItemConfigDao.saveObject(configDO);
		return assembleD2V(newConfigDO);
	}
	/**
	 * 功能描述: 修改装修收费登记项目<br>
	 * @param renovateChargeItemConfigVO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 */
	public RenovateChargeItemConfigVO edit(RenovateChargeItemConfigVO renovateChargeItemConfigVO){
		checkParameterIsNull(renovateChargeItemConfigVO.getKeyword(),"system:approval_info_id_not_null");//选择记录
		checkRenovateChargeItemConfigBaseInfo(renovateChargeItemConfigVO);

		RenovateChargeItemConfigDO configDO = renovateChargeItemConfigDao.findRenovateChargeItemConfigById(renovateChargeItemConfigVO.getKeyword());
		checkParameterIsNull(configDO, "system:approval_info_object_not_found"); //系统没有找到记录

//		processBizCodeV2D(configDO,renovateChargeItemConfigVO); //处理交易码
		configDO.setDescription(renovateChargeItemConfigVO.getDescription());//描述
//		configDO.setItemName(renovateChargeItemConfigVO.getItemName());//项目名称
		configDO.setChargeObject(renovateChargeItemConfigVO.getChargeObject());//收费对象
		configDO.setChargeMode(renovateChargeItemConfigVO.getChargeMode());//计费模式
		configDO.setPrice(renovateChargeItemConfigVO.getPrice()); //收费标准
		configDO.setState(renovateChargeItemConfigVO.getState()); //状态
		configDO.setSysInfo(super.createInfo(renovateChargeItemConfigVO.getModifier())); //系统信息
		return assembleD2V(configDO);
	}
	/**
	 * 功能描述: 删除装修收费登记项目<br>
	 * @param renovateChargeItemConfigVO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 */
	@SuppressWarnings("unchecked")
	public void delete(Long keyword){
		checkParameterIsNull(keyword,"system:approval_info_id_not_null");//选择记录
		RenovateChargeItemConfigDO configDO = renovateChargeItemConfigDao.findRenovateChargeItemConfigById(keyword);
		checkParameterIsNull(configDO, "system:approval_info_object_not_found"); //系统没有找到记录

		renovateChargeItemConfigDao.doDelete(configDO);
	}
	/**
	 * 功能描述:获取所有页面需要的可用的交易码<br>
	 * Create author:yixianjian <br>
	 * Create on:2009-1-4  <br>
	 */
	public List<BusinessCodeVO> getBusinessCode(){
		ParameterDefVO parameter_deposit = parameterDefService.findParameterDefByPkey(this.DEPOSIT_CODE);//获取押金
		if(parameter_deposit == null || parameter_deposit.getPvalue() == null)
			throw new BusinessException("system:renovate_charge_error",BusinessException.ERROR);
		BusinessCodeVO depositCodeVO = businessCodeService.findBusinessCodeByKeyword(parameter_deposit.getPvalue());
		List<BusinessCodeVO> incomeBizCode = businessCodeService.getAllIncomeBusinessCode();
		incomeBizCode.add(depositCodeVO);
		return incomeBizCode;
	}
	/**
	 * 功能描述:验证项目名称不能一样<br>
	 * @param renovateChargeItemConfigVO
	 * Create author:yixianjian <br>
	 * Create on:2009-1-4  <br>
	 */
	public void validateItenName(RenovateChargeItemConfigVO renovateChargeItemConfigVO){
		if(renovateChargeItemConfigVO == null) return ;
		if(BlankUtil.isBlank(renovateChargeItemConfigVO.getItemName())) return ;

		Long keyword = renovateChargeItemConfigVO.getKeyword();
		String itemName = renovateChargeItemConfigVO.getItemName();

		List<RenovateChargeItemConfigDO> appDOS = null;
		if(keyword == null){
			appDOS = renovateChargeItemConfigDao.findByItemName(itemName);
		}else{
			appDOS = renovateChargeItemConfigDao.findByItemNameWithoutKeyword(keyword,itemName);
		}
		if(!BlankUtil.isBlank(appDOS))
			throw new BusinessException("system:renovateChargeItem_itemName_isExist",BusinessException.WARNING);
	}
	/**
	 * 功能描述:验证装修收费项目基本信息<br>
	 * @param renovateChargeItemConfigVO
	 * Create author:yixianjian <br>
	 * Create on:2008-12-31  <br>
	 */
	private void checkRenovateChargeItemConfigBaseInfo(RenovateChargeItemConfigVO renovateChargeItemConfigVO){
		checkParameterIsNull(renovateChargeItemConfigVO, "system:approval_info_not_null"); //输入信息
		checkParameterIsNull(renovateChargeItemConfigVO.getChargeObject(),"system:approval_chargeObject_not_null");//收费对象
		checkParameterIsNull(renovateChargeItemConfigVO.getPrice(),"system:approval_price_not_null");//收费标准
	}
	/**
	 * 功能描述:VO TO DO<br>
	 * @param configVO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-31  <br>
	 */
	private RenovateChargeItemConfigDO assembleV2D(RenovateChargeItemConfigVO configVO){
		if(configVO == null) return null;

		RenovateChargeItemConfigDO configDO = new RenovateChargeItemConfigDO();
		configDO.setKeyword(configVO.getKeyword()); //处理keyword
		processBizCodeV2D(configDO,configVO); //交易码
		configDO.setDescription(configVO.getDescription());//描述
		configDO.setItemName(configVO.getItemName()); //项目名称
		configDO.setChargeObject(configVO.getChargeObject()); //收费对象
		configDO.setChargeMode(configVO.getChargeMode()); //收费模式
		configDO.setPrice(configVO.getPrice()); //收费标准
		configDO.setState(configVO.getState()); //使用状态

		return configDO;
	}
	/**
	 * 功能描述: 将VO中的交易码初始化到DO<br>
	 * @param configDO
	 * @param configVO
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 */
	private void processBizCodeV2D(RenovateChargeItemConfigDO configDO,RenovateChargeItemConfigVO configVO){
		BusinessCodeDomain businessCode = new BusinessCodeDomain();
		businessCode.setKeyword(configVO.getBizKeyword());
		configDO.setBusinessCode(businessCode);
	}

	/**
	 * 功能描述:DO转VO<br>
	 * @param renovateChargeItemConfigDO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 */
	private List<RenovateChargeItemConfigVO> assembleD2V(List<RenovateChargeItemConfigDO> dos){
		if(BlankUtil.isBlank(dos)) return null;

		List<RenovateChargeItemConfigVO> vos = new ArrayList<RenovateChargeItemConfigVO>();
		for(RenovateChargeItemConfigDO ricDO : dos){
			vos.add(assembleD2V(ricDO));
		}
		return vos;
	}
	/**
	 * 功能描述:DO转VO<br>
	 * @param renovateChargeItemConfigDO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 */
	private RenovateChargeItemConfigVO assembleD2V(RenovateChargeItemConfigDO renovateChargeItemConfigDO){
		if(renovateChargeItemConfigDO == null) return null;

		RenovateChargeItemConfigVO rciVO = new RenovateChargeItemConfigVO();

		rciVO.setKeyword( renovateChargeItemConfigDO.getKeyword() );
		rciVO.setBizKeyword( renovateChargeItemConfigDO.getBusinessCode().getKeyword() ); //交易码ID
		rciVO.setBizCode( renovateChargeItemConfigDO.getBusinessCode().getBizCode() ); //交易码名称
		rciVO.setDescription( renovateChargeItemConfigDO.getBusinessCode().getDescription()); //描述
		rciVO.setItemName( renovateChargeItemConfigDO.getItemName() ); //收费项目名称
		rciVO.setChargeObject( renovateChargeItemConfigDO.getChargeObject() ); //收费对象
		rciVO.setChargeMode( renovateChargeItemConfigDO.getChargeMode() ); //计费模式
		rciVO.setPrice(renovateChargeItemConfigDO.getPrice()); //收费标准
		rciVO.setState(  renovateChargeItemConfigDO.getState() ); //使用状态
		return rciVO;
	}

	public void setRenovateChargeItemConfigDao(
			IRenovateChargeItemConfigDao renovateChargeItemConfigDao) {
		this.renovateChargeItemConfigDao = renovateChargeItemConfigDao;
	}
	public void setParameterDefService(IParameterDefService parameterDefService) {
		this.parameterDefService = parameterDefService;
	}
	public void setBusinessCodeService(IBusinessCodeService businessCodeService) {
		this.businessCodeService = businessCodeService;
	}
}
